package com.yunxin.websitebox.website.controller;

import com.yunxin.websitebox.commons.AjaxResult;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@Slf4j
public class IndexController {
    @GetMapping(value = {"/","/index"})
    public String index(){
        return "index";
    }


    @GetMapping(value = "/index.html")
    public String wepage(){
        return "html/index";
    }

    @PostMapping("/login")
    @ResponseBody
    public AjaxResult login(String username, String password) {
        UsernamePasswordToken token = new UsernamePasswordToken(username, password);
        Subject subject = SecurityUtils.getSubject();
        try {
            subject.login(token);
            log.info("登录成功!");
            return AjaxResult.ok().message("登录成功!");
        } catch (UnknownAccountException e) {
            return AjaxResult.error().message("用户名或密码不正确!");
        } catch (IncorrectCredentialsException e) {
            return AjaxResult.error().message("用户名或密码不正确!");
        }
    }

    @GetMapping("/unauth")
    public String unauth() {
        return "unauth";
    }

    @GetMapping("/p404")
    public String error404(){
        return "error/error_404";
    }

    @GetMapping("/p500")
    public String error500(){
        return "error/error_500";
    }

    @GetMapping("/p403")
    public String error403(){
        return "error/error_403";
    }
}
